﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GJ_Model.Interface;
using GJ_Model.Info;
using System.Data;


namespace GJ_Model.BE
{
    /// <summary>
    /// 公交数据操作类
    /// </summary>
   public class GJDataOperation
    {
       /// <summary>
       /// 公交所有数据(按公交线路分组）
       /// </summary>
       public static Dictionary<string, string[]> GJAllDataGJName
       {
           get;
           set;
       }
       /// <summary>
       /// 公交所有数据（按公交站名分组）
       /// </summary>
       public static Dictionary<string, string[]> GJAllDataStation
       {
           get;
           set;
       }

       public void GetAllData(DataTable dtAll)
       {
           try
           {
               GJAllDataGJName = new Dictionary<string, string[]>();
               GJAllDataStation = new Dictionary<string, string[]>();
               if (dtAll != null && dtAll.Rows.Count > 0)
               {
                   string[,] stas;
                   int stasLen = 0;
                   List<string[,]> listStas = new List<string[,]>();
                   for (int i = 0; i < dtAll.Rows.Count; i++)
                   {
                       DataRow dr = dtAll.Rows[i];
                       string[] sta=GetGJStationsByValues(dr["GJValues"].ToString());
                       GJAllDataGJName.Add(dr["GJName"].ToString(),sta);
                       string[,] staa=getGJStationsByValues(dr["GJValues"].ToString(),dr["GJName"].ToString());
                       stasLen += staa.Length / 2;
                       listStas.Add(staa);
                   }
                   stas = new string[stasLen, 2];
                   int stasIndex=0;
                   int itemIndex = 0;
                   foreach (string[,] item in listStas)
                   {
                       for (int i = 0; i < item.Length/2; i++)
                       {
                           stas[itemIndex+i, 0] = item[i, 0];
                           stas[itemIndex + i, 1] = item[i, 1];
                       }
                       itemIndex += item.Length / 2;
                     stasIndex++;
                   }

                   for (int i = 0; i < stas.Length/2; i++)
                   {
                       if (!GJAllDataStation.Keys.Contains(stas[i, 0]))
                       {
                           GJAllDataStation.Add(stas[i, 0], GetGJNameByStation(stas[i, 0], stas));
                       }
                   }
               }
           }
           catch (Exception ex)
           {
               throw ex;
           }
       }

       private string[] GetGJStationsByValues(string gjValues)
       {
           try
           {
               if (!string.IsNullOrEmpty(gjValues))
               {
                  string [] sta=gjValues.Split('-');
                  return sta;
               }
               return null; 
           }
           catch (Exception ex)
           {
               throw ex;
           }
       }

       private string[,] getGJStationsByValues(string gjValues, string gjName)
       {
           try
           {
               string[] sta=gjValues.Split('-');
               string[,] staa = new string[sta.Length, 2];
               for (int i = 0; i < sta.Length; i++)
               {
                   staa[i, 0] = sta[i];
                   staa[i, 1] = gjName;
               }
               return staa;
           }
           catch (Exception ex)
           {
               throw ex;
           }
       }

       private string[] GetGJNameByStation(string gjStation, string[,] allStation)
       {
           try
           {
               string[] GJNames;
               List<string> list = new List<string>();
               for (int i = 0; i < allStation.Length/2; i++)
               {
                   if(gjStation.Equals(allStation[i,0]))
                   {
                       list.Add(allStation[i, 1]);
                   }
               }
               GJNames = new string[list.Count];
               for (int i = 0; i < list.Count; i++)
               {
                   GJNames[i] = list[i];
               }
               return GJNames;
           }
           catch (Exception ex)
           {
               throw ex; 
           }
       }
    }
}
